/**
 * 集成测试文件解析接口测试用例
 *
 * Created by nongzhizhong on 2021/11/25.
 */
const chai = require('chai');
const should = chai.should(); // eslint-disable-line no-unused-vars
chai.use(require('chai-http'));
const common = require('../../../common');
const Administrator = require('../../../../models/administrator');
const File = require('../../../../models/file');
const uri = require('../../uri');
const rules = require('../../../../benchmark/static-analyze/gjb.json');
const benchmark = require('./../../../../benchmark/integration-test/main');
module.exports = describe('/PUT files/:id.json', function () {
  const administrator = new Administrator();
  let integrationTestVersion = {};
  let staticAnalyzeTestVersion = {};
  let project = {};
  let newUser;
  const newUserIds = [];
  function url(fileId) {
    if (!fileId) {
      fileId = integrationTestVersion.rootDirectory.fileId;
    }
    return `/files/${fileId}.json`;
  }

  before(function (done) {
    administrator.login()
      .then(() => administrator.createRandomTester())
      .then((tester) => {
        newUser = tester;
        newUserIds.push(tester.userId);
        return tester.createRandomProject();
      })
      .then((res) => {
        project = res;
        // 初始化集成测试
        return project.createRandomIntegrationVersion(benchmark.files);
      })
      .then((res) => {
        integrationTestVersion = res;
        // 初始化静态分析版本
        return project.createIntegrationTestStaticAnalyzeVersion(
          res.masterVersionId,
          newUser,
          rules.verified_rules
        );
      })
      .then((res) => {
        staticAnalyzeTestVersion = res;
        done();
      })
      .catch(done);
  });
  after(function (done) {
    administrator.removeUsers(newUserIds)
      .then(() => {
        done();
      })
      .catch(done);
  });

  common.should.be.user(uri, url, 'PUT');
  common.should.be.tester(uri, url, 'PUT');
  // 集成测试添加文件
  it('it should ADD a file to integrationTest', function (done) {
    let fileId;
    let integrationTestFiles;
    integrationTestVersion.rootDirectory.createEmptyFile()
      .then((file) => {
        fileId = file.fileId;
        file.fileId = integrationTestVersion.rootDirectory.fileId;
        return file.getTree();
      })
      .then((files) => {
        integrationTestFiles = files;
        const { rootDirectory } = staticAnalyzeTestVersion;
        const file = new File(
          staticAnalyzeTestVersion,
          rootDirectory.fileId,
          rootDirectory.path,
          rootDirectory.status,
          rootDirectory.kind,
        );
        return file.getTree();
      })
      .then((files) => {
        chai.request(uri)
          .get(url(fileId))
          .set('Authorization', `Bearer ${project.creator.token.accessToken}`)
          .end((err, res) => {
            res.should.have.status(200);
            files.total.should.be.equal(integrationTestFiles.total);
            done();
          });
      })
      .catch(done);
  });
  // 集成测试正常删除文件;
  it('it should DELETE a file of integration test version', function (done) {
    let fileId;
    let originalFileTotal;
    integrationTestVersion.rootDirectory.getTree()
      .then((files) => {
        fileId = files.tree.children[0].fileId;
        originalFileTotal = files.total;
        const { rootDirectory } = staticAnalyzeTestVersion;
        const file = new File(
          staticAnalyzeTestVersion,
          rootDirectory.fileId,
          rootDirectory.path,
          rootDirectory.status,
          rootDirectory.kind,
        );
        return file.getTree();
      })
      .then((staticAnalyzeFiles) => {
        chai.request(uri)
          .delete(url(fileId))
          .set('Authorization', `Bearer ${project.creator.token.accessToken}`)
          .end((err, res) => {
            res.should.have.status(204);
            // 查询
            chai.request(uri)
              .get(url(fileId))
              .set('Authorization', `Bearer ${project.creator.token.accessToken}`)
              .end((err, res) => {
                res.should.have.status(404);
                common.should.be.errorMessage(res.body);
                staticAnalyzeFiles.total.should.be.eql(originalFileTotal);
                done();
              });
          });
      })
      .catch(done);
  });
});
